Introduction

Column

Get to know the data

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Example of a figure using plot_ly function

Column

Motivation

You can talk about the motivation of the studay and introduce your data here! Remove Column {.tabset data-width=400} this line and the corresponding tags in your rmarkdown file if you don’t want to have several tages here!

Here is a list of themes you can use for dashboard:

A variety of themes are available to modify the base appearance of flexdashboard. Available themes include:

  • default

  • cosmo

  • bootstrap

  • cerulean

  • journal

  • flatly

  • readable

  • spacelab

  • united

  • lumen

  • paper

  • sandstone

  • simplex

  • yeti

Tag 1

Tag 2

Data Exploration

Column

Some figures

You can put figure(s) here!

Column

Some explanations regarding the figures you have

Diagnostics

Column

Linearity

Normality

Equality Variance

Residuals vs Leverage

Column

First, we load the necessary packages and data. Fit a linear model to the data and obtain necessary objects for diagnostics plots.

1. Linearity Assumption

We check the linearity assumption: there is a linear relationship between…

2. Normality Assumption

We check the normality assumption:….

Section 4

Column

Figure on the first row and first column

Figure on the second row and the first column

Column

Figure on the first row and the second column

Figure on the second row and the second column

Section 5

Row

Another section

Put something here!

Row

Put something here!

Put something in this area!

Row 1

Put something in row 1.

Row 2

Put something in row 2.

---
title: "Template 1"
author: "Tessa Chen"
output: 
  flexdashboard::flex_dashboard:
    theme: simplex
    orientation: columns
    social: ["facebook", "twitter", "linkedin"]
    source_code: embed
---

```{r setup, include=FALSE}
# load necessary packages
library(ggplot2)
library(plotly)
library(plyr)
library(flexdashboard)  ## you need this package to create dashboard

# read the data set here, I use data: mtcars as an example
df <- mtcars
```

Introduction
=======================================================================

Column {data-width=600}
-----------------------------------------------------------------------

### Get to know the data

```{r}
head(df)
```


### Example of a figure using plot_ly function

```{r}
df$am <- as.factor(df$am)
p <- plot_ly(df, x = ~mpg, color = ~ifelse(df$am==0, "automatic", "manual"), type = "box")
ggplotly(p)

```

Column {.tabset data-width=400} 
-----------------------------------------------------------------------

### Motivation

You can talk about the motivation of the studay and introduce your data here! Remove *Column {.tabset data-width=400}* this line and the corresponding tags in your rmarkdown file if you don't want to have several tages here!

Here is a list of themes you can use for dashboard:

A variety of themes are available to modify the base appearance of flexdashboard. Available themes include:

- default

- cosmo

- bootstrap

- cerulean

- journal

- flatly

- readable

- spacelab

- united

- lumen

- paper

- sandstone

- simplex

- yeti

### Tag 1
```{r}
plot(rnorm(100))
```

### Tag 2
```{r}
plot(rnorm(100))
```


Data Exploration
=======================================================================

Column {data-width=500}
-----------------------------------------------------------------------

### Some figures
You can put figure(s) here!

Column {data-width=500}
-----------------------------------------------------------------------
### Some explanations regarding the figures you have

Diagnostics
=======================================================================
Column {.tabset data-width=650}
-----------------------------------------------------------------------

```{r}
df <- mtcars
df$am <- as.factor(df$am)
fit <- lm(mpg ~ wt + disp + am, data=df)

#obtain values needed in order to get diagnostics plots
# Extract fitted values
Fitted.Values <- fit$fitted.values

# Extract residuals
Residuals <- fit$residuals

# Calculate standardized residuals 
Standardized.Residuals <- scale(fit$residuals)

# Extract fitted values for lm() object
Theoretical.Quantiles <- qqnorm(Residuals, plot.it = F)$x

# find Square root of abs(residuals)
Root.Residuals <- sqrt(abs(Standardized.Residuals))

# Calculate Leverage
Leverage <- lm.influence(fit)$hat

# Create data frame 
# Will be used as input to plot_ly

diagnostics <- data.frame(Fitted.Values, 
                     Residuals, 
                     Standardized.Residuals, 
                     Theoretical.Quantiles,
                     Root.Residuals,
                     Leverage)
```
### Linearity

```{r}
m <- list(
  l = 100,
  r = 100,
  b = 100,
  t = 100,
  pad = 4
)

# Fitted vs Residuals
p1 <- plot_ly(diagnostics, x = Fitted.Values, y = Residuals, 
          type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data", 
          marker = list(size = 10, opacity = 0.5))%>%
  layout(title = "Residuals vs Fitted Values", 
       xaxis = list(title="Fitted Values", font=list(size=14)), 
       yaxis = list(title="Residuals", font=list(size=14)), 
       plot_bgcolor = "#e6e6e6", 
       font=list(size=14), margin=m)

ggplotly(p1)
```

### Normality
```{r}
# QQ Pot
p2 <- plot_ly(diagnostics, x = Theoretical.Quantiles, y = Standardized.Residuals, type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data", marker = list(size = 10, opacity = 0.5), showlegend = F)%>%
    add_trace(x = Theoretical.Quantiles, y = Theoretical.Quantiles, type = "scatter", mode = "line", name = "", line = list(width = 2))%>%
  layout(title = "Q-Q Plot", plot_bgcolor = "#e6e6e6",
              xaxis = list(title="Theoretical Quantiles", font=list(size=14)), 
       yaxis = list(title="Standardized Residuals", font=list(size=14)), font=list(size=14), margin=m)

ggplotly(p2)
```

### Equality Variance
```{r}
# Scale Location
p3 <- plot_ly(diagnostics, x = Fitted.Values, y = Root.Residuals, 
          type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data",
          marker = list(size = 10, opacity = 0.5), showlegend = F)%>%
  layout(title = "Scale-Location", plot_bgcolor = "#e6e6e6", xaxis = list(title="Fitted Values", font=list(size=14)), 
       yaxis = list(title=expression(sqrt("|Standardized Residuals|")), font=list(size=14)), font=list(size=14), margin=m)

ggplotly(p3)
```

### Residuals vs Leverage
```{r}
s <- loess.smooth(Leverage, Residuals)
p4 <- plot_ly(diagnostics, x = Leverage, y = Residuals, 
            type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data", marker = list(size = 10, opacity = 0.5), showlegend = F) %>% 
    add_trace(x = s$x, y = s$y, type = "scatter", mode = "line", name = "Smooth", line = list(width = 2)) %>% 
    layout(title = "Leverage vs Residuals", plot_bgcolor = "#e6e6e6", xaxis = list(title="Leverage", font=list(size=14)), 
       yaxis = list(title="Residuals", font=list(size=14)), font=list(size=14), margin=m)
ggplotly(p4)  
```


Column {data-width=350}
-----------------------------------------------------------------------
 
First, we load the necessary packages and data. Fit a linear model to the data and obtain necessary objects for diagnostics plots. 

**1. Linearity Assumption**

We check the linearity assumption: there is a linear relationship between... 

**2. Normality Assumption**

We check the normality assumption:....


Section 4 
=======================================================================

Column
-----------------------------------------------------------------------

### Figure on the first row and first column

```{r}
dfGamma = data.frame(nu75 = rgamma(100, 0.75),
           nu1 = rgamma(100, 1),
           nu2 = rgamma(100, 2))

dfGamma = stack(dfGamma)

p <- ggplot(dfGamma, aes(x = values)) +
            stat_density(aes(group = ind, color = ind),position="identity",geom="line")
ggplotly(p)
```

### Figure on the second row and the first column

```{r}
x <- rnorm(100)
hist(x)
```

Column
-----------------------------------------------------------------------

### Figure on the first row and the second column
```{r}
dd<-data.frame(matrix(rnorm(144, mean=2, sd=2),72,2),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(dd) <- c("x_value", "Predicted_value",  "State_CD")

dd <- data.frame(
  predicted = rnorm(72, mean = 2, sd = 2),
  state = rep(c("A", "B", "C"), each = 24)
)

grid <- with(dd, seq(min(predicted), max(predicted), length = 100))
normaldens <- ddply(dd, "state", function(df) {
  data.frame(
    predicted = grid,
    density = dnorm(grid, mean(df$predicted), sd(df$predicted))
  )
})

p <- ggplot(dd, aes(predicted))  +
            geom_density() +
            geom_line(aes(y = density), data = normaldens, colour = "red") +
            facet_wrap(~ state)
ggplotly(p)
```

### Figure on the second row and the second column

```{r}
df <- data.frame(x <- rchisq(1000, 10, 10),
                 y <- rnorm(1000))

p <- ggplot(df, aes(x, y)) + 
     geom_point(alpha = 0.5) + 
     geom_density_2d() + 
     theme(panel.background = element_rect(fill = '#ffffff'))

ggplotly(p)
```

Section 5
=======================================================================

Row {data-hight=650}
-----------------------------------------------------------------------
### Another section
Put something here!

Row {data-height=350}
-----------------------------------------------------------------------

### Put something here!
Put something in this area!

### Row 1
Put something in row 1.

### Row 2
Put something in row 2.